#[derive(Debug)]
pub struct Stack<T> {
    top: Option<Box<StackNode<T>>>,
}

#[derive(Clone,Debug)]
struct StackNode<T> {
    val: T,
    next: Option<Box<StackNode<T>>>,
}

impl <T> StackNode<T> {
    fn new(val: T) -> StackNode<T> {
        StackNode { val: val, next: None }
    }
}

impl<T> Stack<T> {
    pub fn new() -> Stack<T> {
        Stack{ top: None }
    }
    pub fn push(&mut self, val: T) {
        let mut node = StackNode::new(val);
        let next = self.top.take();
        node.next = next;
        self.top = Some(Box::new(node));
    }
    pub fn pop(&mut self) -> Option<T> {
        let val = self.top.take();
        match val {
            None => None,
            Some(mut x) => {
                self.top = x.next.take();
                Some(x.val)
            },
        }
    }

    pub fn get_top(&self) -> Option<&T> {
        match &self.top {
            None => None,
            Some(x) => {
                Some(&x.val)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn it_works() {
        let mut s = Stack::new();
        s.push(1);
        let sn = s.pop();
        assert_eq!(sn, Some(1));
    }

    #[test]
    fn get_top_works() {
        let mut s = Stack::new();
        s.push(1);
        let sn = s.get_top();
        assert_eq!(sn, Some(&1));
    }
}
